using System.Collections;
using System.Linq;
using ArticleManagementSystem.Api.Entity;
using ArticleManagementSystem.Api.Repostisory;
using Microsoft.AspNetCore.Mvc;

namespace ArticleManagementSystem.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class CarouselsController : ControllerBase
    {
        private readonly IRepostisory<Carousel> _carouselRepostisory;
        private readonly IRepostisory<Imgs> _imgsRepostisory;

        public CarouselsController(IRepostisory<Carousel> carouselRepostisory, IRepostisory<Imgs> imgsRepostisory)
        {
            _carouselRepostisory = carouselRepostisory;
            _imgsRepostisory = imgsRepostisory;
        }

        [HttpGet, Route("list")]
        public dynamic GetCarousels()
        {
            var Carousels = _carouselRepostisory.Table.ToList();
            var imgs = _imgsRepostisory.Table;
            ArrayList list = new ArrayList();
            foreach (var item in Carousels)
            {
                var img = imgs.Where(x => x.Id == item.ImgId).FirstOrDefault();
                var tmp = new
                {
                    id = item.Id,
                    ArticleId = item.ArticleId,
                    imgPath = img != null ? img.ImgsPath : ""
                };

                list.Add(tmp);
            }

            return new
            {

                code = 200,
                data = list,
                msg = "获取轮播图成功"
            };

        }
    }
}